package com.wyix.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import lombok.Data;
import lombok.ToString;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author wangdx
 * @since 2025/8/26
 */
@Data
@Entity
@Table(name = "t_book")  // 数据库表名
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)  // 二级缓存：读写策略
@ToString(exclude = {"borrowRecords", "collectors"})
public class Book {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)  // 主键自增
    private Long id;

    @Column(name = "book_name", length = 100, nullable = false)  // 图书名称（非空）
    private String bookName;

    @Column(name = "author", length = 50, nullable = false)  // 作者
    private String author;

    @Column(name = "isbn", length = 20, unique = true, nullable = false)  // ISBN（唯一）
    private String isbn;

    @Column(name = "price")  // 价格
    private BigDecimal price;

    @Column(name = "stock")  // 库存数量
    private Integer stock = 0;

    @Column(name = "publish_time")  // 出版时间
    private Date publishTime;

    @Column(name = "create_time", updatable = false)  // 创建时间（不可更新）
    @Temporal(TemporalType.TIMESTAMP)
    private Date createTime = new Date();

    // 关联：一本书被多个用户借阅（一对多）
    @OneToMany(mappedBy = "book", cascade = CascadeType.ALL, orphanRemoval = true,fetch = FetchType.EAGER)
    @JsonIgnore
    private List<BorrowRecord> borrowRecords;

    // 关联：一本书被多个用户收藏（多对多，中间表 t_user_book_collection）
    @ManyToMany(mappedBy = "collectedBooks",fetch = FetchType.EAGER)
    @JsonIgnore
    private List<User> collectors;
}
